const isPromise = (obj) => {
    return obj && typeof obj.then === 'function'
}

const promiseMiddleWare = ({dispatch, getState}) => {
    return (next) => (action) => {
        const {types, promise} = action
        if(!isPromise(promise) && !(promise && types.length == 3)) {
            return next(action)
        }
        
        const [PENDING,DONE,FAIL] = types
        return  promise
                .then(res => {
                    dispatch({type: DONE, payload: res})
                })
                .catch(e => {
                    dispatch({type: FAIL, payload: e})
                })
    }
} 

export default promiseMiddleWare